package mqtt

import (
	"encoding/json"
	MQTT "github.com/eclipse/paho.mqtt.golang"
	"miomp-terminal-suite/backend/pkg/mqtt"
)

func InitSubscribe() {
	mqtt.AddCloudSubHandlers(TEST)
}

func handlerMessage[T any](msgContent T, mqttMsg MQTT.Message) (*mqtt.Message[T], error) {
	msg := &mqtt.Message[T]{
		Duplicate: mqttMsg.Duplicate(),
		Qos:       mqttMsg.Qos(),
		Retained:  mqttMsg.Retained(),
		Topic:     mqttMsg.Topic(),
		MessageID: mqttMsg.MessageID(),
	}
	err := json.Unmarshal(mqttMsg.Payload(), &msgContent)
	if err != nil {
		return msg, err
	}
	msg.Payload = msgContent
	return msg, nil
}

var (
	TEST = &mqtt.SubHandler{Topic: "test", Qos: 1, MsgHandler: baseDashboard.HandlerTestMes()}
)
